package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketCollect;
import com.cskaoyan.market.db.domain.MarketCollectExample;
import com.cskaoyan.market.db.mapper.MarketCollectMapper;
import com.cskaoyan.market.service.AdminCollectService;
import com.cskaoyan.market.util.MybatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Created with IntelliJ IDEA 2021.
 * User: Lazarus_Liu
 * Date: 2023/7/24
 * Time: 20:22
 * Description:
 */
public class AdminCollectServiceImpl implements AdminCollectService {
    @Override
    public List<MarketCollect> list(String page, String limit, String userId, String valueId, String sort, String order) {
        // 1. 获取sqlSession
        SqlSession session = MybatisUtil.getSession();
        MarketCollectMapper mapper = session.getMapper(MarketCollectMapper.class);

        // 2. 增加筛选条件
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        // 当用户Id(userId)不为空时查询
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        // 当商品Id(valueId)不为空时查询
        if (!StringUtils.isEmpty(valueId)) {
            criteria.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        // 3. 增加排序方式
        marketCollectExample.setOrderByClause(sort + " " + order);
        // 4. 分页
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketCollect> marketCollects = mapper.selectByExample(marketCollectExample);
        // 5. 提交事务
        session.commit();
        session.close();
        // 6. 返回list给Controller
        return marketCollects;
    }
}
